"""
df: OHLC
open high low close pre_close
1. 连涨天数
2 连跌天数
3 oprelose
4. hpreclose

"""
import pandas as pd
import datetime
import numpy as np


class analysis_helper:
    def __init__(self, df_source):
        self.df = df_source
        
    def init_df(self):
        f_time = lambda x:  datetime.datetime.strptime(x, "%Y%m%d")
        self.df.trade_date = self.df.trade_date.apply(f_time)        
        self.df.set_index('trade_date', inplace=True)
        # self['rtn'] = np.log(df.close) - np.log(df.close.shift(1))
        self.df['rtn'] = np.log(self.df.close) - np.log(self.df.pre_close)
        
        
        
    def GetAnalyInfo(self):
        self.df['o2pre'] = pd.DataFrame(self.df.open - self.df.pre_close)        
        self.df['h2pre'] = pd.DataFrame(self.df.high - self.df.pre_close)
        
        h_b = self.df.h2pre[self.df.h2pre > 0].count()
        h_zero = self.df.h2pre[self.df.h2pre == 0].count()
        h_s = self.df.h2pre[self.df.h2pre < 0].count()
        
        
        o_b = self.df.o2pre[self.df.o2pre > 0].count()
        o_zero = self.df.o2pre[self.df.o2pre == 0].count()
        o_s = self.df.o2pre[self.df.o2pre < 0].count()
        
        
        self.destdf = pd.DataFrame([[h_b,h_zero,h_s],[o_b,o_zero,o_s]],index=['high','open'],columns=['bigger','zero', 'small'])
        
        return self.destdf
